//
//  AdVertiseViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/19.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "AdVertiseViewController.h"
#import "ViewController.h"
#import <WKWebViewJavascriptBridge.h>

@interface AdVertiseViewController ()

@property (nonatomic, strong) UIButton *countBtn;

@property (nonatomic, strong) NSTimer *countTimer;//定时器

@property (nonatomic, assign) int count;

@end

static int const showTime = 3;//显示时间

@implementation AdVertiseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIImage *img = [UIImage imageNamed:@"laImage"];
    self.view.layer.contents = (id)img.CGImage;
    //跳过广告
    self.countBtn = [[UIButton alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.countBtn];
    [self.countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(40);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
    
    //自定义的跳过按钮
    [self.countBtn addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
    [self.countBtn setTitle:[NSString stringWithFormat:@"跳过%d",showTime] forState:UIControlStateNormal];
    self.countBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.countBtn.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6];
     self.countBtn.layer.cornerRadius = 4;
    [self startTimer];
    
    //添加手势 点击广告页跳转到广告界面
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToAdWeb)];
    [self.view addGestureRecognizer:tap];
    
}
#pragma mark - 开始计时
- (void)startTimer{
    
    _count = showTime;
    [[NSRunLoop mainRunLoop]addTimer:self.countTimer forMode:NSRunLoopCommonModes];
    
}
#pragma mark - 跳过广告
- (void)dissmiss{
    [self.countTimer invalidate];//
    self.countTimer = nil;
//此处的失败就是创建了一个控制器修改根控制器，可以使用工具类实现功能，不需要修改根控制器
    [UIView animateWithDuration:0.3f animations:^{
        self.view.alpha = 0.f;
    } completion:^(BOOL finished) {
        /** 方法无用
         UIWindow *window = [[UIApplication sharedApplication].delegate window];
                //清空
             if(window.rootViewController){
                 window.rootViewController = nil;
             }
             window.rootViewController = [[ViewController alloc]init];
             [window makeKeyAndVisible];
         */
        ViewController *viewVC = [[ViewController alloc]init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:viewVC];
//        self.view.window.rootViewController = naVC;
        /**
        self.view.window.rootViewController = viewVC;
        self.modalPresentationStyle = UIModalPresentationFullScreen;
         */
//        naVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:naVC animated:NO completion:^{
            [self.view removeFromSuperview];
        }];
    }];
}
#pragma mark - 自动计时
- (void)countDown{
    self.count --;
    [self.countBtn setTitle:[NSString stringWithFormat:@"跳过%d",self.count] forState:UIControlStateNormal];
    if(self.count == 0){
        //计时完成 广告消失
        [self dissmiss];
    }
}
#pragma mark - 跳转到广告页
- (void)jumpToAdWeb{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushtoad" object:nil userInfo:nil];
    [self dissmiss];
    
}
#pragma mark - lazy
- (NSTimer *)countTimer{
    if(!_countTimer){
        _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _countTimer;
}

@end
